/************************************* * File: temp.cpp * Author: Katherine Gibson * Date: 4/19/2016 * Section: N/A * E-mail: k38@umbc.edu * Description: * This program shows the basics of * how to use templated functions. *************************************/ #include // lets us use standard exceptions #include #include using namespace std; /* minVector() is a templated function that returns a vector's minimum * Input: a vector of type template (T) * Output: the minimum value from that vector (of type T) */ template T minVector( vector vec); /* createVectors() populates three vectors for testing * Input: 3 empty vectors (int, string, and char) * Output: 3 vectors populated with multiple entries */ void createVectors(vector &ints, vector &strs, vector &chars); int main() { vector ints; vector strs; vector chars; // if we comment out this line of code, we'll have an exception thrown createVectors(ints, strs, chars); try { cout << "Minimum of ints is " << minVector(ints) << endl; cout << "Minimum of strings is \"" << minVector(strs) << "\"" << endl; cout << "Minimum of chars is '" << minVector(chars) << "'" < T minVector( vector vec) { // if the vector is empty, this will throw an exception T minn = vec.at(0); for (int i = 0; i < (int) vec.size(); i++) { if (vec.at(i) < minn) { minn = vec.at(i); } } return minn; } void createVectors(vector &ints, vector &strs, vector &chars) { // add content to the string vector strs.push_back("yay"); strs.push_back("extension"); strs.push_back("dawgs"); strs.push_back("cattes"); strs.push_back("abcdefghijklmnopqrstuvwxyz"); strs.push_back("aabcdefghijklmnopqrstuvwxyz"); // if we uncomment, this will be the "minimum" string // strs.push_back("YELLING"); // add content to the integer vector ints.push_back(1); ints.push_back(5); ints.push_back(7); ints.push_back(8); ints.push_back(22); ints.push_back(44); ints.push_back(45); ints.push_back(-9); // add content to the character vector chars.push_back('a'); chars.push_back('b'); chars.push_back('z'); chars.push_back('o'); // if we uncomment, this will be the "minimum" char chars.push_back('K'); // if we uncomment, this will be the "minimum" char chars.push_back('!'); // if we uncomment, this will be the "minimum" char chars.push_back(' '); }